package net.lzzy.practices.aspects;

import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.web.models.ApiResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author Jaymo on 2022/5/18
 *
 */

@Component
@Aspect
public class AdminControllerAspect {
    @Pointcut("execution(* net.lzzy.practices.web.admin.*.*(..))")
    private void adminAspect(){}
    @Around("adminAspect()")
    public ApiResult checkAdmin(ProceedingJoinPoint joinPoint) throws Throwable {
        String token = joinPoint.getArgs()[0].toString();
        if (AuthUtils.illegalUser(token, new int[]{User.TYPE_ADMIN})){
            return  new ApiResult(false, Messages.No_PERMISSION.toString(), null);
        }
        return (ApiResult)  joinPoint.proceed();
    }
}
